﻿using Basic.FlowApplication.Interface;
using Basic.FlowCollect;
using Basic.FlowExtendService;
using Basic.FlowLocalEvent.Model;
using Basic.FlowModel.DB;
using Basic.FlowRemoteModel.AuditSignFile.Model;
using WeDonekRpc.Helper;

namespace Basic.FlowApplication.lmpl
{
    internal class FlowAuditSignFileService : IFlowAuditSignFileService
    {
        private readonly IFlowAuditSignFileCollect _SignFile;
        private readonly IFileService _File;
        private readonly IFlowAuditItemLogCollect _Item;
        public FlowAuditSignFileService ( IFlowAuditSignFileCollect signFile, IFileService file, IFlowAuditItemLogCollect item )
        {
            this._Item = item;
            this._File = file;
            this._SignFile = signFile;
        }

        public long Add ( AuditSignFileAdd add )
        {
            long id = this._SignFile.Add(add);
            this._File.Save(add.FileId, id);
            return id;
        }
        public void SetImgUri ( long id, string imgUri, long fileId )
        {
            DBFlowAuditSignFile db = this._SignFile.Get(id);
            string src = db.ImgUri;
            if ( this._SignFile.SetImgUri(db, imgUri) )
            {
                this._File.Save(fileId, id, src);
            }
        }
        public void Delete ( long id )
        {
            DBFlowAuditSignFile db = this._SignFile.Get(id);
            this._SignFile.Delete(db);
            new FlowAuditSignFileEvent(db).Send("Delete");
        }

        public AuditSignFile[] Gets ( long logId )
        {
            return this._SignFile.Gets<AuditSignFile>(logId);
        }

        public void SignFileAccredit ( SignFileAccredit obj )
        {
            AuditSignFile file = this._SignFile.Get<AuditSignFile>(obj.FileId);
            this._Item.CheckIsAccredit(obj.LogId, obj.EmpId);
            if ( !this._File.Accredit(file.FileId, obj.AccreditId) )
            {
                throw new ErrorException("flow.sign.file.accredit.fail");
            }
        }
    }
}
